package com.yjkf.zh.admin.controller;

import com.yjkf.zh.admin.aspect.LogAnnotation;
import com.yjkf.zh.admin.dao.SysDeviceMapper;
import com.yjkf.zh.admin.dao.SysUserDeviceMapper;
import com.yjkf.zh.admin.model.SysDevice;
import com.yjkf.zh.admin.model.SysProduct;
import com.yjkf.zh.admin.service.SysDeviceService;
import com.yjkf.zh.core.http.HttpResult;
import com.yjkf.zh.core.page.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("device")
public class SysDeviceController {

    @Autowired
    private SysDeviceService sysDeviceService;

    @Autowired
    private SysDeviceMapper sysDeviceMapper;

    @Autowired
    private SysUserDeviceMapper sysUserDeviceMapper;


    @PreAuthorize("hasAuthority('sys:device:view')")
    @PostMapping(value = "/findPage")
  //  @LogAnnotation(content = "查看设备列表")
    public HttpResult findPage(@RequestBody PageRequest pageRequest,HttpServletRequest request) {
        Long userId = (Long)request.getSession().getAttribute("userId");
        return HttpResult.ok(sysDeviceService.findPage2(pageRequest,userId));
    }


    @GetMapping("/devices")
    public HttpResult findDevice(@RequestParam Long id){
        return HttpResult.ok(sysDeviceService.findDevices(id));
    }

    //设备选择查询，实时数据的选择查询接口
    @GetMapping("/deviceForUser")
    public HttpResult findDeviceForUser(HttpServletRequest request){
        Long userId = (Long)request.getSession().getAttribute("userId");
      //  return HttpResult.ok(sysDeviceService.findDevices(userId));
        return HttpResult.ok(sysUserDeviceMapper.findByUserId2(userId));
    }

    //设备选择查询，实时数据的选择查询接口
    @GetMapping("/devicesForUser")
    public HttpResult findDevicesForUser(HttpServletRequest request){
        Long userId = (Long)request.getSession().getAttribute("userId");
        return HttpResult.ok(sysUserDeviceMapper.devicesForUser(userId));
    }

    @PreAuthorize("hasAuthority('sys:adminDevice:view')")
    @PostMapping(value = "/deviceForAdmin")
    public HttpResult findPage3(@RequestBody PageRequest pageRequest) {
        return HttpResult.ok(sysDeviceService.findPageDeviceForAdmin(pageRequest));
    }

    @PreAuthorize("hasAuthority('sys:adminDevice:add') AND hasAuthority('sys:adminDevice:edit')")
    @PostMapping(value = "/saveForAdmin")
    @LogAnnotation(content = "增加设备")
    public HttpResult saveForAdmin(@RequestBody SysDevice sysDevice){
        if(sysDevice.getId()==null||sysDevice.getId()==0){
            if(!sysDeviceMapper.findDeviceForIdent(sysDevice.getIdent()).isEmpty()){
                return HttpResult.error("该设备标识已存在！");
            }
            if(!sysDeviceMapper.findByDeviceName(sysDevice.getDeviceName()).isEmpty()){
                return HttpResult.error("该设备名称已经存在！");
            }
        }
        return HttpResult.ok(sysDeviceService.save(sysDevice));
    }

    @PreAuthorize("hasAuthority('sys:device:delete')")
    @PostMapping(value = "/delete")
    @LogAnnotation(content = "删除设备")
    public HttpResult delete(@RequestBody List<SysDevice> sysDevices){
        return HttpResult.ok(sysDeviceService.delete(sysDevices));
    }


}
